package com.quasar.hpatient.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.bumptech.glide.Glide;
import com.quasar.hpatient.R;
import com.quasar.hpatient.choicePhoto.CommonUtils;
import com.quasar.hpatient.model.custommodel.PhotoModel;
import com.quasar.hpatient.view.ImageViewpageActivity_;
import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.List;


public class ImageAdapter extends BaseAdapter implements OnItemClickListener {
    private int itemWidth;
    private int horizentalNum = 3;
    private LayoutParams itemLayoutParams;
    private Context context;
    private ArrayList<PhotoModel> data;

    public ImageAdapter(Context context, ArrayList<PhotoModel> mods) {
        this.context = context;
        this.data = mods;
        setItemWidth(getWidthPixels(context));
    }

    public static int getWidthPixels(Context context) {
        int horizental = context.getResources().getDimensionPixelSize(
                R.dimen.imageadapter);

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels - horizental;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void setItemWidth(int screenWidth) {
        int itemWidth1 = context.getResources().getDimensionPixelSize(
                R.dimen.imageadapter);
        this.itemLayoutParams = new LayoutParams(itemWidth1, itemWidth1);

    }

    ImageView item = null;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        item = new ImageView(context);
        item.setBackgroundColor(Color.parseColor("#00000000"));
        item.setScaleType(ScaleType.CENTER_CROP);
        if (convertView == null) {
            item = new ImageView(context);
            item.setScaleType(ScaleType.CENTER_CROP);
            item.setLayoutParams(itemLayoutParams);
            convertView = item;
        } else {
            item = (ImageView) convertView;
            item.setScaleType(ScaleType.CENTER_CROP);
            item.setLayoutParams(itemLayoutParams);
        }
        if(data.get(position).getOriginalPath().contains("file://")){
            Glide.with(context).load(data.get(position).getOriginalPath()).into(item);
        }else {
            ViseLog.d("图片显示地址"+data.get(position).getOriginalPath());
            Glide.with(context).load(data.get(position).getOriginalPath()).into(item);
        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        ArrayList<String> str=new ArrayList<>();
        ArrayList<PhotoModel> photoModels=new ArrayList<>();
        for(int i=0;i<data.size();i++){
            str.add(data.get(i).getOriginalPath());
            PhotoModel photoModel=new PhotoModel();
            photoModel.setOriginalPath(data.get(i).getOriginalPath());
            photoModels.add(photoModel);
        }
        String stats = "ImageAdapter";
        ImageViewpageActivity_.intent(context).Photolist(str).position(arg2).photoModelDatas(photoModels).STATS(stats).start();
    }

}
